﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace Pong_Project.Views
{
    class Settings : View
    {
        private string header, back, scoreLimit, choosePowerups, chooseLevel, chooseColors;
        private SpriteFont headerFont, textFont;
        private Vector2 headerPos, backPos, scoreLimitPos, choosePowerupsPos, chooseLevelPos, chooseColorsPos;
        private Vector2 headerOrigin, backOrigin, scoreLimitOrigin, choosePowerupsOrigin, chooseLevelOrigin, chooseColorsOrigin;
        private int selectedEntry = 1;
        private int scoreLimitEntry = Score.GetInstance().WinningScore;
        private int minEntry = 1;
        private int maxEntry = 5;
        private static List<String> levelEntries;
        private int levelEntry = 0;

        public Settings()
            : base()
        {
            header = "Settings";
            headerPos = new Vector2(Game.WindowWidth / 2, 20);

            scoreLimit = "Change max points: ";
            scoreLimitPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 150);

            choosePowerups = "Choose Powerups";
            choosePowerupsPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 100);

            chooseLevel = "Choose Level: ";
            chooseLevelPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 125);

            chooseColors = "Choose Colors";
            chooseColorsPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 75);

            back = "Back";
            backPos = new Vector2(Game.WindowWidth / 2, Game.WindowHeight / 2 - 50);
        }

        public override void LoadContent(ContentManager content)
        {
            headerFont = content.Load<SpriteFont>("Text");
            textFont = content.Load<SpriteFont>("Text");
            headerOrigin = headerFont.MeasureString(header);
            headerOrigin = new Vector2(headerOrigin.X / 2, 0); //headerOrigin.Y
            backOrigin = textFont.MeasureString(back) / 2;
            scoreLimitOrigin = textFont.MeasureString(scoreLimit + "< XX >") / 2;
            choosePowerupsOrigin = textFont.MeasureString(choosePowerups) / 2;
            chooseLevelOrigin = textFont.MeasureString(chooseLevel + "< Level X >") / 2;
            chooseColorsOrigin = textFont.MeasureString(chooseColors) / 2;
        }

        public static void SetLevelEntries(int numberOfEntries)
        {
            if (numberOfEntries < 1)
            {
                throw new ArgumentException("Number of level entries in settings cannot be lower than 1");
            }
            levelEntries = new List<string>();
            levelEntries.Add("Level 0");
            for (int i = 1; i < numberOfEntries; i++)
            {
                levelEntries.Add("Level " + i);
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Controls.MuteSound)
            {
                MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            }

            if (Controls.MenuDown)
            {
                if (selectedEntry + 1 > maxEntry)
                    selectedEntry = minEntry;
                else
                    selectedEntry++;
            }
            else if (Controls.MenuUp)
            {
                if (selectedEntry - 1 < minEntry)
                    selectedEntry = maxEntry;
                else
                    selectedEntry--;

            }

            switch (selectedEntry)
            {
                case 1:
                    if (Controls.MenuRight) scoreLimitEntry++;
                    if (Controls.MenuLeft && scoreLimitEntry > 1) scoreLimitEntry--;
                    break;
                case 2:
                    if (Controls.MenuRight)
                        levelEntry = (levelEntry == levelEntries.Count - 1 ? 0 : levelEntry + 1);
                    if (Controls.MenuLeft)
                        levelEntry = (levelEntry == 0 ? levelEntries.Count - 1 : levelEntry - 1);
                    break;
                case 3:
                    if (Controls.Enter)
                        ViewManager.State = GameState.ChoosePowerups;
                    break;
                case 4:
                    if (Controls.Enter)
                        ViewManager.State = GameState.ChooseColors;
                    break;
                case 5:
                    if (Controls.Enter)
                    {
                        Score.GetInstance().WinningScore = scoreLimitEntry;
                        LevelManager.SetActiveLevel(levelEntry);
                        ViewManager.State = GameState.Menu;
                        selectedEntry = 1;
                    }
                    break;
                default:
                    throw new ArgumentException("Invalid menu entry: " + selectedEntry);
            }

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(headerFont, header, headerPos, Color.White, 0, headerOrigin, 1.0f, SpriteEffects.None, 0.5f);

            if (selectedEntry != 1)
                spriteBatch.DrawString(textFont, scoreLimit + "< " + scoreLimitEntry + " >", scoreLimitPos, Color.White, 0, scoreLimitOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 2)
                spriteBatch.DrawString(textFont, chooseLevel + "< " + levelEntries[levelEntry] + " >", chooseLevelPos, Color.White, 0, chooseLevelOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 3)
                spriteBatch.DrawString(textFont, choosePowerups, choosePowerupsPos, Color.White, 0, choosePowerupsOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 4)
                spriteBatch.DrawString(textFont, chooseColors, chooseColorsPos, Color.White, 0, chooseColorsOrigin, 1.0f, SpriteEffects.None, 0.5f);
            if (selectedEntry != 5)
                spriteBatch.DrawString(textFont, back, backPos, Color.White, 0, backOrigin, 1.0f, SpriteEffects.None, 0.5f);

            switch (selectedEntry)
            {
                case 1:
                    spriteBatch.DrawString(textFont, scoreLimit + "< " + scoreLimitEntry + " >", scoreLimitPos, Color.Red, 0, scoreLimitOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 2:
                    spriteBatch.DrawString(textFont, chooseLevel + "< " + levelEntries[levelEntry] + " >", chooseLevelPos, Color.Red, 0, chooseLevelOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 3:
                    spriteBatch.DrawString(textFont, choosePowerups, choosePowerupsPos, Color.Red, 0, choosePowerupsOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 4:
                    spriteBatch.DrawString(textFont, chooseColors, chooseColorsPos, Color.Red, 0, chooseColorsOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                case 5:
                    spriteBatch.DrawString(textFont, back, backPos, Color.Red, 0, backOrigin, 1.0f, SpriteEffects.None, 0.5f);
                    break;
                default:
                    throw new ArgumentException("Invalid menu entry: " + selectedEntry);
            }
        }
    }
}
